﻿// Cpp2LuaGlobalFunc.cpp
//
//  Created by caochuanhui
//
//  Lua and C++/c 交互类

#include "Cpp2LuaGlobalFunc.h"
#include "CCLuaEngine.h"
#include "LuaBasicConversions.h"



namespace cpp2lua{
	static inline void sExecuteScriptFile(const char *pszFilePath)
	{
		CCASSERT(pszFilePath && strlen(pszFilePath) > 0, "WARNING: param(pszFuncName) error in sExecuteScriptFile");
		auto luaStack = cocos2d::LuaEngine::getInstance()->getLuaStack();
		auto sta = cocos2d::LuaEngine::getInstance();
		luaStack->executeScriptFile(pszFilePath);
	}

	static inline void sExecuteGlobalFunctionWithPanelType(const char *pszFuncName, int type, std::string channel, std::string talkText)
	{
		CCASSERT(pszFuncName && strlen(pszFuncName), "WARNING: param(pszFuncName) error in sExecuteGlobalFunctionWithPanelType");
		auto luaStack = cocos2d::LuaEngine::getInstance()->getLuaStack();
		lua_getglobal(luaStack->getLuaState(), pszFuncName);
		luaStack->pushInt(type);
		luaStack->pushString(channel.c_str());
		luaStack->pushString(talkText.c_str());
		//luaStack->executeGlobalFunction(pszFuncName);
		// 通过调用方法executeFunction可以在控制台输出错误信息，用于追踪调试信息
		luaStack->executeFunction(3);
	}



#define EXECUTE_CPP_2_LUA_GLOBAL_FILE	sExecuteScriptFile("src/app/utils/Cpp2LuaGlobalFunc.lua")
#define EXECUTE_CPP_2_LUA_GLOBAL_FILE64	sExecuteScriptFile("src64/app/utils/Cpp2LuaGlobalFunc.lua")

	/**************************************************************************************************
	//C++ 调用 Lua 接口;
	**************************************************************************************************/

	void gluaAllCallBack(std::string funName, CCArray* r)
	{
        
        bool bit64 = false;
        if (sizeof(long) == 4) {
            bit64 = false;
        } else {
            bit64 =  true;
        }
        if(bit64 == true)
            EXECUTE_CPP_2_LUA_GLOBAL_FILE64;
		else
            EXECUTE_CPP_2_LUA_GLOBAL_FILE;
        auto luaStack = cocos2d::LuaEngine::getInstance()->getLuaStack();
		lua_getglobal(luaStack->getLuaState(), funName.c_str());
		CCObject* it;
		int count = 0;


	/*	DictElement* element;
		CCDICT_FOREACH(r, element)
		{
			int key = element->getIntKey();
			if (key >= LUADATACHAR && key < LUADATINT)
			{
				String* str = (String*)element->getObject();
				luaStack->pushString(str->getCString());
				count++;
			}
			else if (key >= LUADATINT && key < LUADATAFLOAT)
			{
				Integer* inte = (Integer*)element->getObject();
				luaStack->pushInt(inte->getValue());
				count++;
			}
			else if (key >= LUADATAFLOAT && key < LUADATALONG)
			{
				Float* fl = (Float*)element->getObject();
				luaStack->pushFloat(fl->getValue());
				count++;
			}
			else if (key >= LUADATALONG && key < LUADATABOOL)
			{
				Double* dou = (Double*)element->getObject();
				luaStack->pushLong(dou->getValue());
				count++;
			}
			else if (key >= LUADATABOOL)
			{
				Bool* bo = (Bool*)element->getObject();
				luaStack->pushBoolean(bo->getValue());
				count++;
			}
		}*/
		/*	if (r->objectForKey(LUADATACHAR))
			{

			}*/
		CCARRAY_FOREACH(r, it)
		{
			LuaData* data = (LuaData*)it;
			if (LUADATINT == data->dataType)
			{
				int i = data->m_inter;
				luaStack->pushInt(i);
				count++;
			}
			else if (LUADATABOOL == data->dataType)
			{
				bool i = data->m_bool;
				luaStack->pushBoolean(i);
				count++;
			}
			else if (LUADATACHAR == data->dataType)
			{
				std::string i = data->m_str;
				luaStack->pushString(i.c_str());
				count++;
			}
			else if (LUADATALONG == data->dataType)
			{
				long i = data->m_long;
				luaStack->pushLong(i);
				count++;
			}
			else if (LUADATAFLOAT == data->dataType)
			{
				float i = data->m_float;
				luaStack->pushFloat(i);
				count++;
			}

		}

		luaStack->executeFunction(count);


	}
}
